package com.teacher.service.impl;

import com.teacher.entity.Evaluation;
import com.teacher.repository.EvaluationRepository;
import com.teacher.service.EvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class EvaluationServiceImp implements EvaluationService {
    @Autowired
    EvaluationRepository evaluationRepository;

    /**
     * 增加
     * @param Evaluation
     * @return
     */
    @Override
    public Evaluation addEvaluation(Evaluation Evaluation) {
        return evaluationRepository.save(Evaluation);
    }

    /**
     * 修改
     * @param Evaluation
     * @return
     */
    @Override
    public Evaluation updateEvaluation(Evaluation Evaluation) {
        return evaluationRepository.saveAndFlush(Evaluation);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public Evaluation getDetailsById(String id) {

        return evaluationRepository.findById(Integer.valueOf(id)).get();
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteEvaluation(String id) {
        Evaluation Evaluation = null;
        try{
            Evaluation = evaluationRepository.findById(Integer.valueOf(id)).get();
            evaluationRepository.delete(Evaluation);
        }catch (RuntimeException e){
            e.printStackTrace();
        }
    }

    /**
     * 批量删除
     * @param idList
     */
    @Override
    public void deleteEvaluationList(String idList) {
        String[] split = idList.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        evaluationRepository.deleteEvaluationsByIdIn(list);
    }

    /**
     * 根据条件分页查询
     * @param evaluation,page,size
     */
    @Override
    public Page<Evaluation> specificPageQueryEvaluationList(Evaluation evaluation, int page, int size) {
        Specification querySpecification = (Specification<Evaluation>) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<Predicate>();
            if(evaluation != null)
            {
                // 学年学期
                String academicYear = evaluation.getAcademicYear();
                if (!StringUtils.isEmpty(academicYear)) {
                    predicates.add(cb.equal(root.get("academicYear").as(String.class), academicYear));
                }
            }

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return evaluationRepository.findAll(querySpecification,PageRequest.of(page-1,size));
    }
}
